package org.csu.mypetstore.persistence;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository
public interface CartMapper   {
    @Update("update cart set quantity = #{quantity},totalcost = #{totalCost} where username = #{username} and itemid = #{itemId}")
    boolean updateItemByUsernameAndItemId(String username, String itemId, int quantity, BigDecimal totalCost);

    @Select("select * from cart where username = #{username} and itemid = #{itemId}")
    CartItem selectItemByUsernameAndItemId(String username, String itemId);

    @Insert("insert into cart(username,itemid,instock,quantity,totalcost,pay)\n" +
            "        values(#{username},#{itemId},#{instock},#{quantity},#{totalCost},false)")
    int insertItemByUsernameAndItemId(String username, String itemId, boolean instock, int quantity, BigDecimal totalCost);

    @Select("select * from cart where username = #{username}")
    List<Cart> selectItemByUsername(String username);

    @Delete("delete from cart where username = #{username} and itemid = #{itemId}")
    void removeItemByUsernameAndItemId(String username, String itemId);

    @Update("update cart set pay = #{pay} where username = #{username} and itemId = #{itemId}")
    void updateItemByItemIdAndPay(String username, String itemId, boolean pay);
}
